package com.tjetc.test.common;

import java.util.List;

/**
 * @author gaoyongxiang
 * @version v1.0
 * @date 2025/9/9
 */
public class PageResult<T> {

    // 当前页码
    private int page;
    // 每页大小
    private int size;
    // 总记录数
    private long total;
    // 总页数
    private int totalPages;
    // 数据列表
    private T data;
    // 是否有上一页
    private boolean hasPrevious;
    // 是否有下一页
    private boolean hasNext;
    // 是否是第一页
    private boolean isFirst;
    // 是否是最后一页
    private boolean isLast;

    public PageResult() {
    }

    public PageResult(int page, int size, long total, T data) {
        this.page = page;
        this.size = size;
        this.total = total;
        this.data = data;
        // 计算剩余参数
        calculateResult();
    }

    private void calculateResult() {
        // 计算总页面数
        totalPages = (int) Math.ceil((double)total / size);

        // 边界处理
        if (totalPages == 0) {
            totalPages = 1;
        }

        if (page > totalPages) {
            page = totalPages;
        }

        if (page < 1) {
            page = 1;
        }

        hasPrevious = page > 1;
        hasNext = page < totalPages;
        isFirst = page == 1;
        isLast = page == totalPages;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isHasPrevious() {
        return hasPrevious;
    }

    public void setHasPrevious(boolean hasPrevious) {
        this.hasPrevious = hasPrevious;
    }

    public boolean isHasNext() {
        return hasNext;
    }

    public void setHasNext(boolean hasNext) {
        this.hasNext = hasNext;
    }

    public boolean isFirst() {
        return isFirst;
    }

    public void setFirst(boolean first) {
        isFirst = first;
    }

    public boolean isLast() {
        return isLast;
    }

    public void setLast(boolean last) {
        isLast = last;
    }
}